import QtQuick 2.15
import QtQuick.Controls 2.15

Item {
    id: processListViewBack
    width: 1900
    height: 920

    // Rectangle{
    //     anchors.fill: parent
    //     radius: 5
    //     color: "red"
    // }

    // 初始化数据库和加载数据
    Component.onCompleted: {
        // processManager.initTable();
        processManager.getAllData();
        // // // showStatus("正在从数据库加载数据...");
    }

    property real recHeight: 40
    property real recWidth: 94
    property real fontSize: 26
    property real showFontSize: 20
    property int selectedIndex: -1  // 跟踪当前选中项索引
    property var currentItem: null  // 当前选中的数据项

    // 写入参数合集
    property string write_Process1
    property string write_Process2
    property string write_Fu_AE_knife_1
    property string write_Fu_AE_knife_2
    property string write_Bu_AE_knife_1
    property string write_Bu_AE_knife_2
    property string write_Fu_knife_1
    property string write_Fu_knife_2
    property string write_Bu_knife_1
    property string write_Bu_knife_2
    property string write_Fl_knife_1
    property string write_Fl_knife_2
    property string write_Bl_knife_1
    property string write_Bl_knife_2
    property string write_Roller

    // 当前位置合集
    property string currentLocation_Process1:Cpp_PLC.Entry_1_CurrLoc
    property string currentLocation_Process2:Cpp_PLC.Entry_2_CurrLoc
    property string currentLocation_FU_AE_knife_1:Cpp_PLC.FU_knife_1_A_E_CurrLoc
    property string currentLocation_FU_AE_knife_2:Cpp_PLC.FU_knife_2_A_E_CurrLoc
    property string currentLocation_BU_AE_knife_1:Cpp_PLC.BU_knife_1_A_E_CurrLoc
    property string currentLocation_BU_AE_knife_2:Cpp_PLC.BU_knife_2_A_E_CurrLoc
    property string currentLocation_FU_knife_1:Cpp_PLC.FU_knife_1_CurrLoc
    property string currentLocation_FU_knife_2:Cpp_PLC.FU_knife_2_CurrLoc
    property string currentLocation_BU_knife_1:Cpp_PLC.BU_knife_1_CurrLoc
    property string currentLocation_BU_knife_2:Cpp_PLC.BU_knife_2_CurrLoc
    property string currentLocation_FL_knife_1:Cpp_PLC.FD_knife_1_CurrLoc
    property string currentLocation_FL_knife_2:Cpp_PLC.FD_knife_2_CurrLoc
    property string currentLocation_BL_knife_1:Cpp_PLC.BD_knife_1_CurrLoc
    property string currentLocation_BL_knife_2:Cpp_PLC.BD_knife_2_CurrLoc
    property string currentLocation_Roller:Cpp_PLC.Idler_CurrLoc



    Row{
        z:10
        Rectangle{
            border.width: 1 ;width: 70 ;height: recHeight;
            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("序号")
            }
        }
        Rectangle{
            border.width: 1
            width: 200
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("名  称")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("工进1")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("工进2")
            }

        }

        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("压辊")
            }
        }

        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前1辅")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前2辅")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后1辅")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后2辅")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前上1")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前上2")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后上1")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后上2")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前下1")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("前下2")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后下1")
            }

        }
        Rectangle{
            border.width: 1
            width: recWidth
            height: recHeight

            Text{
                anchors.centerIn: parent
                font.pixelSize: fontSize
                text: qsTr("后下2")
            }

        }

    }

    ListView{
        id: materialListView
        anchors.top: processListViewBack.top
        anchors.topMargin: 40
        anchors.left: processListViewBack.left
        width: 1680
        height: 880
        model:processModel
        boundsBehavior: Flickable.StopAtBounds
        // 监听数据库数据加载完成
        Connections {
            target: processManager
            function onDataLoaded(model) {
                processModel.copyModel(model);
                // showStatus("数据已同步");
            }
        }

        // 监听模型数据变化
        Connections {
            target: processModel
            function onModelDataUpdated() {
                materialListView.currentIndex = -1;
                selectedIndex = -1;
                editPopup.close();
            }
        }

        // 仅跟踪索引，不直接操作currentItem
        onCurrentIndexChanged: {
            selectedIndex = currentIndex;
        }

        delegate: Item {
            width: 1680
            height: 45

            // 双击编辑
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    materialListView.currentIndex = index;
                }
                onDoubleClicked: {
                    // 双击时验证索引有效性并打开弹窗
                    if (index >= 0 && index < processModel.count()) {
                        selectedIndex = index;
                        populateEditForm(index);  // 传入索引而非item
                        editPopup.open();
                    } else {
                        console.warn("无效的列表项索引:", index);
                    }
                }
            }

            Row {
                id: dataRow
                height: parent.height

                // 序号列
                Rectangle {
                    width: 70
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: (index + 1)
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 名称列
                Rectangle {
                    width: 200
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: Name || qsTr("未命名")
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        width: parent.width - 8
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 工进1列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: Process1 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 工进2列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: Process2 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 压辊列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: Roller || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前上刀1辅助列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_UpperKnife01Assist || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前上刀2辅助列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_UpperKnife02Assist || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后上刀1辅助列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_UpperKnife01Assist || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后上刀2辅助列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_UpperKnife02Assist || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前上刀1列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_UpperKnife01 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前上刀2列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_UpperKnife02 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后上刀1列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_UpperKnife01 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后上刀2列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_UpperKnife02 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前下刀1列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_LowerKnife01 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 前下刀2列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: F_LowerKnife02 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后下刀1列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_LowerKnife01 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                // 后下刀2列
                Rectangle {
                    width: recWidth
                    height: parent.height
                    color: materialListView.currentIndex === index ? "gold" : "white"
                    border.color: "#eeeeee"
                    Text {
                        anchors.centerIn: parent
                        text: B_LowerKnife02 || ""
                        font.pixelSize: showFontSize
                        elide: Text.ElideRight
                        horizontalAlignment: Text.AlignHCenter
                    }
                }                
            }
        }

        ScrollBar.vertical: ScrollBar {
            policy: ScrollBar.AlwaysOn
            width: 10
            anchors.right: parent.right
        }
    }



    Column{
        anchors.top: processListViewBack.top
        anchors.topMargin: 30
        anchors.right: processListViewBack.right
        anchors.rightMargin: 5
        spacing: 10

        A_Button{
            contentText_Data: qsTr("增加数据")
            onButtonClicked: {
                processManager.addData();
            }
        }
        A_Button{
            contentText_Data: qsTr("下载参数")
            buttonEnable: selectedIndex !== -1 && selectedIndex < processModel.count()
            onButtonClicked: {
                if (selectedIndex !== -1 && selectedIndex < processModel.count()) {
                    // 获取当前选中项数据
                    const selectedItem = processModel.get(selectedIndex);
                    if (selectedItem) {
                        // 输出所选行的参数到控制台
                        // console.log("当前所选行参数：");
                        // console.log(`工进1: ${selectedItem.Process1 || ""}`);
                        // console.log(`工进2: ${selectedItem.Process2 || ""}`);
                        // console.log(`前1辅: ${selectedItem.F_UpperKnife01Assist || ""}`);
                        // console.log(`前2辅: ${selectedItem.F_UpperKnife02Assist || ""}`);
                        // console.log(`后1辅: ${selectedItem.B_UpperKnife01Assist || ""}`);
                        // console.log(`后2辅: ${selectedItem.B_UpperKnife02Assist || ""}`);
                        // console.log(`前上1: ${selectedItem.F_UpperKnife01 || ""}`);
                        // console.log(`前上2: ${selectedItem.F_UpperKnife02 || ""}`);
                        // console.log(`后上1: ${selectedItem.B_UpperKnife01 || ""}`);
                        // console.log(`后上2: ${selectedItem.B_UpperKnife02 || ""}`);
                        // console.log(`前下1: ${selectedItem.F_LowerKnife01 || ""}`);
                        // console.log(`前下2: ${selectedItem.F_LowerKnife02 || ""}`);
                        // console.log(`后下1: ${selectedItem.B_LowerKnife01 || ""}`);
                        // console.log(`后下2: ${selectedItem.B_LowerKnife02 || ""}`);
                        // console.log(`压辊: ${selectedItem.Roller || ""}`);

                        write_Process1 = selectedItem.Process1 === "" ? "0" : selectedItem.Process1
                        write_Process2 = selectedItem.Process2 === "" ? "0" : selectedItem.Process2
                        write_Fu_AE_knife_1 = selectedItem.F_UpperKnife01Assist === "" ? "0" : selectedItem.F_UpperKnife01Assist
                        write_Fu_AE_knife_2 = selectedItem.F_UpperKnife02Assist === "" ? "0" : selectedItem.F_UpperKnife02Assist
                        write_Bu_AE_knife_1 = selectedItem.B_UpperKnife01Assist === "" ? "0" : selectedItem.B_UpperKnife01Assist
                        write_Bu_AE_knife_2 = selectedItem.B_UpperKnife02Assist === "" ? "0" : selectedItem.B_UpperKnife02Assist
                        write_Fu_knife_1 = selectedItem.F_UpperKnife01 === "" ? "0" : selectedItem.F_UpperKnife01
                        write_Fu_knife_2 = selectedItem.F_UpperKnife02 === "" ? "0" : selectedItem.F_UpperKnife02
                        write_Bu_knife_1 = selectedItem.B_UpperKnife01 === "" ? "0" : selectedItem.B_UpperKnife01
                        write_Bu_knife_2 = selectedItem.B_UpperKnife02 === "" ? "0" : selectedItem.B_UpperKnife02
                        write_Fl_knife_1 = selectedItem.F_LowerKnife01 === "" ? "0" : selectedItem.F_LowerKnife01
                        write_Fl_knife_2 = selectedItem.F_LowerKnife02 === "" ? "0" : selectedItem.F_LowerKnife02
                        write_Bl_knife_1 = selectedItem.B_LowerKnife01 === "" ? "0" : selectedItem.B_LowerKnife01
                        write_Bl_knife_2 = selectedItem.B_LowerKnife02 === "" ? "0" : selectedItem.B_LowerKnife02
                        write_Roller = selectedItem.Roller === "" ? "0" : selectedItem.Roller

                        Cpp_PLC.write_into_tcp_Hold_15_Parameters(write_Process1,
                                                                  write_Process2,
                                                                  write_Fu_AE_knife_1,
                                                                  write_Fu_AE_knife_2,
                                                                  write_Bu_AE_knife_1,
                                                                  write_Bu_AE_knife_2,
                                                                  write_Fu_knife_1,
                                                                  write_Fu_knife_2,
                                                                  write_Bu_knife_1,
                                                                  write_Bu_knife_2,
                                                                  write_Fl_knife_1,
                                                                  write_Fl_knife_2,
                                                                  write_Bl_knife_1,
                                                                  write_Bl_knife_2,
                                                                  write_Roller)


                    } else {
                        console.log("无法获取选中项数据");
                    }
                } else {
                    console.log("未选中任何行");
                }
            }
        }
        A_Button{
            contentText_Data: qsTr("删除所选")
            buttonEnable: selectedIndex !== -1 && selectedIndex < processModel.count()
            onButtonClicked: {
                if (selectedIndex !== -1 && selectedIndex < processModel.count()) {
                    // 获取选中项ID并删除
                    const selectedItem = processModel.get(selectedIndex);
                    processManager.deleteData(selectedItem.id);
                    selectedIndex = -1;
                    // // // showStatus("数据已删除");
                }
            }
        }

        // 搜索输入框
        Rectangle {
            width: 200
            height: 50
            border.width: 1
            border.color: "#cccccc"

            TextField {
                id: searchInput
                anchors.fill: parent
                padding: 8
                font.pixelSize: fontSize
                placeholderText: qsTr("输入关键词搜索...")
                onAccepted: {
                    // 按回车键也能搜索
                    processManager.searchData(searchInput.text);
                    focus = false
                }
                onTextChanged: {
                    // 清空输入时自动显示全部数据
                    if (text.trim() === "") {
                        processManager.searchData(searchInput.text);
                        // processManager.getAllData();
                    }
                }
            }
        }

        // 搜索按钮
        Rectangle {
            width: 100
            height: 50
            color: "#2196F3"
            border.width: 1
            border.color: "#1976D2"

            Text {
                anchors.centerIn: parent
                color: "white"
                font.pixelSize: fontSize
                text: qsTr("搜索")
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    parent.forceActiveFocus()
                    processManager.searchData(searchInput.text);
                }
            }
        }

        // 重置按钮
        Rectangle {
            width: 100
            height: 50
            color: "#f44336"
            border.width: 1
            border.color: "#d32f2f"

            Text {
                anchors.centerIn: parent
                color: "white"
                font.pixelSize: fontSize
                text: qsTr("重置")
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    searchInput.text = "";
                    // searchKeyword = "";
                    parent.forceActiveFocus()
                    processManager.searchData(searchInput.text);
                    // processManager.getAllData(); // 显示全部数据
                }
            }
        }

        A_Button{
            contentText_Data: qsTr("上传参数")
            buttonEnable: selectedIndex !== -1 && selectedIndex < processModel.count()
            onButtonClicked: {
                console.log(currentLocation_Process1,
                            currentLocation_Process2,
                            currentLocation_FU_AE_knife_1,
                            currentLocation_FU_AE_knife_2,
                            currentLocation_BU_AE_knife_1,
                            currentLocation_BU_AE_knife_2,
                            currentLocation_FU_knife_1,
                            currentLocation_FU_knife_2,
                            currentLocation_BU_knife_1,
                            currentLocation_BU_knife_2,
                            currentLocation_FL_knife_1,
                            currentLocation_FL_knife_2,
                            currentLocation_BL_knife_1,
                            currentLocation_BL_knife_2,
                            currentLocation_Roller)

                const currentItem = processModel.get(selectedIndex);

                console.log(currentItem.id)


                processManager.saveData(currentItem.id, "Process1", currentLocation_Process1);
                processManager.saveData(currentItem.id, "Process2", currentLocation_Process2);
                processManager.saveData(currentItem.id, "F_UpperKnife01Assist", currentLocation_FU_AE_knife_1);
                processManager.saveData(currentItem.id, "F_UpperKnife02Assist", currentLocation_FU_AE_knife_2);
                processManager.saveData(currentItem.id, "B_UpperKnife01Assist", currentLocation_BU_AE_knife_1);
                processManager.saveData(currentItem.id, "B_UpperKnife02Assist", currentLocation_BU_AE_knife_2);
                processManager.saveData(currentItem.id, "F_UpperKnife01", currentLocation_FU_knife_1);
                processManager.saveData(currentItem.id, "F_UpperKnife02", currentLocation_FU_knife_2);
                processManager.saveData(currentItem.id, "B_UpperKnife01", currentLocation_BU_knife_1);
                processManager.saveData(currentItem.id, "B_UpperKnife02", currentLocation_BU_knife_2);
                processManager.saveData(currentItem.id, "F_LowerKnife01", currentLocation_FL_knife_1);
                processManager.saveData(currentItem.id, "F_LowerKnife02", currentLocation_FL_knife_2);
                processManager.saveData(currentItem.id, "B_LowerKnife01", currentLocation_BL_knife_1);
                processManager.saveData(currentItem.id, "B_LowerKnife02", currentLocation_BL_knife_2);
                processManager.saveData(currentItem.id, "Roller", currentLocation_Roller);

                processManager.getAllData();

            }
        }

    }


    // 编辑弹窗 - 宽度1680，高度100
    Popup {
        id: editPopup
        width: 1890
        height: 100
        modal: true
        focus: true
        x: (processListViewBack.width - width) / 2
        y: (processListViewBack.height - height) / 2
        // backgroundColor: "#f0f0f0"
        // border.width: 1
        // border.color: "#cccccc"

        // 编辑表单内容
        Row {
            id: editForm
            anchors.fill: parent
            spacing: 0

            // 名称输入框
            Rectangle {
                width: 200
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: nameEdit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("名称")
                }
            }

            // 工进1输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: process1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("工进1")
                }
            }

            // 工进2输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: process2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("工进2")
                }
            }

            // 压辊输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: rollerEdit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("压辊")
                }
            }

            // 前上刀1辅助输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fAssist1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前1辅")
                }
            }

            // 前上刀2辅助输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fAssist2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前2辅")
                }
            }

            // 后上刀1辅助输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bAssist1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后1辅")
                }
            }

            // 后上刀2辅助输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bAssist2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后2辅")
                }
            }

            // 前上刀1输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fUpper1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前上1")
                }
            }

            // 前上刀2输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fUpper2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前上2")
                }
            }

            // 后上刀1输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bUpper1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后上1")
                }
            }

            // 后上刀2输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bUpper2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后上2")
                }
            }

            // 前下刀1输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fLower1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前下1")
                }
            }

            // 前下刀2输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: fLower2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("前下2")
                }
            }

            // 后下刀1输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bLower1Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后下1")
                }
            }

            // 后下刀2输入框
            Rectangle {
                width: 105
                height: parent.height
                border.width: 1
                border.color: "#cccccc"
                TextField {
                    id: bLower2Edit
                    anchors.fill: parent
                    padding: 8
                    font.pixelSize: fontSize
                    placeholderText: qsTr("后下2")
                }
            }            

            // 保存按钮
            Rectangle {
                width: 100
                height: parent.height
                color: "#2196F3"
                border.width: 1
                border.color: "#1976D2"
                Text {
                    anchors.centerIn: parent
                    color: "white"
                    font.pixelSize: fontSize
                    text: qsTr("保存")
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        saveEditedData();
                    }
                }
            }
        }
    }

    // 填充编辑表单数据（通过索引获取数据）
    function populateEditForm(index) {
        if (index < 0 || index >= processModel.count()) {
            console.warn("无效索引，无法填充表单");
            editPopup.close();
            return;
        }

        const item = processModel.get(index);
        if (!item) {
            console.warn("无法获取索引为", index, "的数据项");
            editPopup.close();
            return;
        }

        console.log("当前 ID " + item.id)

        nameEdit.text = item.Name || "";
        process1Edit.text = item.Process1 || "";
        process2Edit.text = item.Process2 || "";
        fAssist1Edit.text = item.F_UpperKnife01Assist || "";
        fAssist2Edit.text = item.F_UpperKnife02Assist || "";
        bAssist1Edit.text = item.B_UpperKnife01Assist || "";
        bAssist2Edit.text = item.B_UpperKnife02Assist || "";
        fUpper1Edit.text = item.F_UpperKnife01 || "";
        fUpper2Edit.text = item.F_UpperKnife02 || "";
        bUpper1Edit.text = item.B_UpperKnife01 || "";
        bUpper2Edit.text = item.B_UpperKnife02 || "";
        fLower1Edit.text = item.F_LowerKnife01 || "";
        fLower2Edit.text = item.F_LowerKnife02 || "";
        bLower1Edit.text = item.B_LowerKnife01 || "";
        bLower2Edit.text = item.B_LowerKnife02 || "";
        rollerEdit.text = item.Roller || "";
    }

    // 保存编辑后的数据 - 关键修复：属性名映射检查与日志跟踪
    function saveEditedData() {
        if (selectedIndex === -1 || selectedIndex >= processModel.count()) {
            console.warn("保存失败：选中项索引无效");
            editPopup.close();
            return;
        }

        const currentItem = processModel.get(selectedIndex);
        if (!currentItem || !currentItem.id) {
            console.warn("保存失败：无法获取当前数据项ID");
            editPopup.close();
            return;
        }

        // 定义所有属性的映射关系（确保与数据库列名完全一致）
        const properties = [
                             { name: "Name", input: nameEdit },
                             { name: "Process1", input: process1Edit },
                             { name: "Process2", input: process2Edit },
                             { name: "F_UpperKnife01Assist", input: fAssist1Edit },
                             { name: "F_UpperKnife02Assist", input: fAssist2Edit },
                             { name: "B_UpperKnife01Assist", input: bAssist1Edit },
                             { name: "B_UpperKnife02Assist", input: bAssist2Edit },
                             { name: "F_UpperKnife01", input: fUpper1Edit },
                             { name: "F_UpperKnife02", input: fUpper2Edit },
                             { name: "B_UpperKnife01", input: bUpper1Edit },
                             { name: "B_UpperKnife02", input: bUpper2Edit },
                             { name: "F_LowerKnife01", input: fLower1Edit },
                             { name: "F_LowerKnife02", input: fLower2Edit },
                             { name: "B_LowerKnife01", input: bLower1Edit },
                             { name: "B_LowerKnife02", input: bLower2Edit },
                             { name: "Roller", input: rollerEdit }
                         ];

        try {
            let log = `保存ID: ${currentItem.id}\n`;

            // 循环保存所有属性（替代逐个保存，减少错误）
            properties.forEach(prop => {
                                   const value = prop.input.text.trim();
                                   processManager.saveData(currentItem.id, prop.name, value);
                                   log += `${prop.name}: ${value}\n`;
                               });

            // 输出调试日志
            // debugLog.text = log;
            console.log("所有属性保存完成:", log);

            // 强制重新加载数据
            processManager.getAllData();
            editPopup.close();
        } catch (e) {
            // console.error("保存数据失败:", e);
            // debugLog.text = `保存失败: ${e.message}`;
        }
    }
}













